package com.ywjt.platformbusiness.cart;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.cart.adapter.CartStoreAdapter;
import com.ywjt.platformbusiness.classify.activity.ShopHomeDetailActivity;
import com.ywjt.platformbusiness.classify.model.HomeRecommonShopModel;
import com.ywjt.platformbusiness.classify.model.ShopListModel;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.home.adapter.HomeRecommondStoreAdapter;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CartIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private List<String> mTitleList;
    private RecyclerView rcCart;
    private CartStoreAdapter cartStoreAdapter;
    private LinearLayout llNodata;
    private TextView tvDelete,tvEdit;
    private LinearLayout llPay;

    private Boolean isDeleteStatus=false;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvEdit:
                if (isDeleteStatus){
                    //删除状态
                    llPay.setVisibility(View.VISIBLE);
                    tvDelete.setVisibility(View.GONE);
                    tvEdit.setText("编辑");
                }else {
                    llPay.setVisibility(View.GONE);
                    tvDelete.setVisibility(View.VISIBLE);
                    tvEdit.setText("完成");
                }
                isDeleteStatus=!isDeleteStatus;
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            getCartData();
        }
    }

    @Override
    protected void onFragmentFirstVisible() {
    }

    public static CartIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
     CartIndex fl = new CartIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.cart_index, container, false);
            context = getActivity();
            initView(view);
        }
        return view;
    }

    public void initView(View view){
        rcCart=view.findViewById(R.id.rcCart);
        llNodata=view.findViewById(R.id.llNodata);
        tvDelete=view.findViewById(R.id.tvDelete);
        llPay=view.findViewById(R.id.llPay);
        tvEdit=view.findViewById(R.id.tvEdit);
        tvEdit.setOnClickListener(this);
        mTitleList=new ArrayList<>();
        for (int i=0;i<3;i++){
            mTitleList.add("");
        }

//        llNodata.setVisibility(View.GONE);
//        rcCart.setVisibility(View.VISIBLE);


    }


    //购物车列表
    private void getCartData() {
        new HttpRequest(getContext()).doGet(UrlConstants.getShopCarListByUser, "",new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), CartListModel.class);
                        if (obj instanceof CartListModel) {
                            CartListModel model=(CartListModel)obj;
                            if (model.getResult()!=null&&model.getResult().size()>0){
                                llNodata.setVisibility(View.GONE);
                                rcCart.setVisibility(View.VISIBLE);
                                cartStoreAdapter=new CartStoreAdapter(model.getResult());
                                rcCart.setLayoutManager(new LinearLayoutManager(context));
                                rcCart.setAdapter(cartStoreAdapter);
                            }else {
                                llNodata.setVisibility(View.VISIBLE);
                                rcCart.setVisibility(View.GONE);
                            }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }
}